package org.example.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Calendar;

@Controller
public class LoginController {

    @RequestMapping("tologin")
    public String toGetIds(  Integer[] userids  ) {
        System.out.println(userids);
        return "";
    }


    @RequestMapping("tologin")
    public String toGetId(
            @RequestParam("id") Integer userid,
            @RequestParam("name") Integer suibianname

    ) {
        System.out.println(userid);
        System.out.println(suibianname);
        return "";
    }


    //    重定向  当我的登录的用户名或密码输入错误，则要求重新登录
    @RequestMapping("error")
    public String tologinRedirect() {
        return "redirect:tologin";
    }

//    请求转发 在新跳转的页面中需要用到以前的页面的操作数据，此时用转发，可以把数据携带过去
    @RequestMapping("tologin")
    public String tologinForWard(Model model) {
        model.addAttribute("username","zhangsan");
        return "index";
    }


    // todo 下面2个方法的效果一模一样
//    @RequestMapping("tologin1")
//    public String tologin3(Model model){
//        model.addAttribute("username","zhangsan");
//        return  "login.html";
//    }
//
//    @RequestMapping("tologin1")
//    public ModelAndView tologin3(){
//        ModelAndView modelAndView = new ModelAndView();
//        //2、携带数据跳转页面
//        modelAndView.addObject("username", "zhangsan");
//        //3、设置要跳转的路径页面
//        modelAndView.setViewName("login.html");
//        return  modelAndView;
//    }


    @RequestMapping("tologin1")
    public String tologin1() {
        return "";
    }

    @RequestMapping("tologin2")
    public void tologin2() {
    }

    @RequestMapping("tologin")
    public ModelAndView tologin(HttpServletRequest request,
                                HttpServletResponse response,
                                InputStream inputStream,
                                OutputStream outputStream,
                                HttpSession httpSession,
                                Model model,
                                ModelAndView modelAndView
    ) {
        //设置要跳转的路径页面
        modelAndView.setViewName("login.html");
        return modelAndView;
    }


    @RequestMapping("login")
    public ModelAndView hello(HttpServletRequest request) {

        System.out.println("进了login");
        ModelAndView modelAndView = new ModelAndView();

        //1、获取请求参数
        String username = request.getParameter("username");
        System.out.println("username =>" + username);

        //2、携带数据跳转页面
        modelAndView.addObject("username", username);

        //3、设置要跳转的路径页面
        modelAndView.setViewName("index.html");

        return modelAndView;

    }

}
